# Setup environment ----
library(data.table)
library(ggplot2)
library(cowplot)
source("functions.R")
theme_set(theme_minimal() + theme(
  strip.background = element_rect(fill = alpha("white", 0.0),
    color = alpha("white", 0.0)),
  axis.title = element_text(size = 10), axis.text = element_text(size = 8),
  axis.ticks.x = element_blank(), plot.title = element_text(hjust = 0.5),
  panel.border = element_rect(color = "black", linewidth = .2, fill = NA),
  strip.text = element_text(size = 10), legend.position = "bottom",
  panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank()))
load("analysis_data.RData")
load("results-2023-08-04.RData")

# Figure 1: Overall Results ----
ggplot(receiver_data, aes(Target, Action)) +
  geom_abline(slope = 1, intercept = 0, linetype = "dotted", color = "gray") +
  geom_point(alpha = .4) +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(~ Condition) +
  xlab("Target") +
  ylab("Action") +
  ggtitle("Overall Results") +
  coord_equal()
ggsave("figure-overall.pdf", width = 6.5, height = 6.5, units = "in")

# Figure 2: How Text Messages Changed Outcomes ----

suppressWarnings(cowplot::ggdraw() +
    cowplot::draw_plot(
      ggplot(sender_payoff_effects, aes(variable, estimate)) +
        geom_hline(yintercept = 0, linetype = 2, color = "gray") +
        geom_errorbar(aes(ymin = q25, ymax = q75), width = 0) +
        geom_point() +
        xlab("") +
        ylab("Average Effect on Sender Payoffs"),
      x = 0, y = 2/3*.9, width = 1, height = 1/3*.9) +
    cowplot::draw_plot(
      ggplot(receiver_payoff_effects, aes(variable, estimate)) +
        geom_hline(yintercept = 0, linetype = 2, color = "gray") +
        geom_errorbar(aes(ymin = q25, ymax = q75), width = 0) +
        geom_point() +
        xlab("") +
        ylab("Average Effect on Receiver Payoffs"),
      x = 0, y = 1/3 * .9, width = 1, height = 1/3*.9) +
    cowplot::draw_plot(
      ggplot(pareto_effects, aes(variable, estimate)) +
        geom_hline(yintercept = 0, linetype = 2, color = "gray") +
        geom_errorbar(aes(ymin = q25, ymax = q75), width = 0) +
        geom_point() +
        xlab("") +
        ylab("Average Effect on Pareto"),
      x = 0, y = 0/3 * .9, width = 1, height = 1/3*.9) +
    cowplot::draw_label(
      paste0("How Text Messages Changed Outcomes"),
      size = 18, x = .5, y = .92,
      fontface = "bold",
      fontfamily = theme_get()$text[["family"]]) +
    theme_nothing())
ggsave2("figure-ames.pdf", width = 9, height = 9, units = "in")
